<?php

namespace App\Http\Resources\Index\Chart;

use App\Http\Resources\Resource;
use Helper\Timed;

class AmountResource extends Resource
{

    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        $amounts = array_column($this->resource->toArray(), 'amount', 'timed');

	    if (Timed::getTimezoneName() == 'America/Scoresbysund') {
		    foreach ($amounts as $k => $v) {
			    $charts['timed'][] = date('Y.m.d', $k);
			    $charts['data'][] = $v;
		    }
	    } else {
		    $timedEnd = Timed::getTodayTimed();
		    $timed = $timedEnd - 86400 * 99;

		    for (; $timed <= $timedEnd; $timed += 86400) {
			    $charts['timed'][] = date('Y.m.d', $timed);
			    $charts['data'][] = $amounts[$timed] ?? 0;
		    }
	    }


        return $this->getResponse()->setResponseData($charts);
    }
}
